package com.concerneyes.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.model.MyGlassManModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2017/6/30 0030.
 */

public class MyUserControlAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<MyGlassManModel> manModels;
    private OnEditClick onEditClick;

    public MyUserControlAdapter(Context context, OnEditClick onEditClick) {
        this.context = context;
        this.onEditClick = onEditClick;
        manModels = new ArrayList<>();
    }

    public void clear() {
        manModels.clear();
        notifyDataSetChanged();
    }


    public void addDatas(List<MyGlassManModel> manModel) {
        if (manModel.size() > 0) {
            manModels.addAll(manModel);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return manModels.size();
    }

    @Override
    public MyGlassManModel getItem(int position) {
        return manModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_control, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.mEditOptometry = (TextView) convertView.findViewById(R.id.edit_optometry);
            holder.mTvSex = (TextView) convertView.findViewById(R.id.tv_sex);
            holder.mTvAge = (TextView) convertView.findViewById(R.id.tv_age);
            holder.mTvJob = (TextView) convertView.findViewById(R.id.tv_job);
            holder.mTvIsdrive = (TextView) convertView.findViewById(R.id.tv_isdrive);
            holder.mTvIscom = (TextView) convertView.findViewById(R.id.tv_iscom);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Calendar mycalendar = Calendar.getInstance();//获取现在时间
        int nowyear = Integer.parseInt(String.valueOf(mycalendar.get(Calendar.YEAR)));//获取年份
        MyGlassManModel data = manModels.get(position);
        holder.tvName.setText(data.getRealName());
        holder.mTvSex.setText(data.getGender());
        holder.mTvAge.setText(String.valueOf(nowyear - Integer.parseInt(data.getYear())) + "岁");
        holder.mTvJob.setText(data.getJob());
        holder.mTvIsdrive.setText(data.getDriveFrequency());
        if (data.isIsUseComputer()) {
            holder.mTvIscom.setText(parent.getResources().getString(R.string.often));
        } else {
            holder.mTvIscom.setText(parent.getResources().getString(R.string.un_often));
        }
        holder.mEditOptometry.setOnClickListener(this);
        holder.mEditOptometry.setTag(position);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_optometry:
                onEditClick.editClick((Integer) v.getTag());
                break;
        }
    }

    public interface OnEditClick {
        void editClick(int position);
    }

    static class ViewHolder {
        TextView tvName;
        TextView mEditOptometry;
        TextView mTvSex;
        TextView mTvAge;
        TextView mTvJob;
        TextView mTvIsdrive;
        TextView mTvIscom;
    }

}
